import { Injectable } from '@nestjs/common';
import axios from 'axios';

@Injectable()
export class ServiceRegistry {
  private readonly registryUrl = 'http://localhost:3005';

  async register(serviceName: string, port: number) {
    await axios.post(`${this.registryUrl}/register`, {
      serviceName,
      url: `http://localhost:${port}`,
    });
  }

  async discover(serviceName: string) {
    const response = await axios.get(
      `${this.registryUrl}/discover?service=${serviceName}`,
    );
    return response.data;
  }
}
